<?php

class FinanceiroDao extends Dao {

    /**
     * @var ClienteService
     */
    private $clienteService;

    public function injectDependencies() {
        $this->clienteService = ClienteService::getInstance();
    }

    public function generateFinancialHistoryReport($IDFinanceiro) {
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlGenerateFinancialReport($IDFinanceiro);
        $this->tMysqli->exec($StSql);
        $Report = $this->tMysqli->fetchAll();
        $this->closeConnection();
        return $Report;
    }

    public function getReceivedTotalValueFromFinancial($IDFinanceiro) {
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlTotalReceivedValueFromFinancial($IDFinanceiro);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'ReceivedTotalValue');
    }

    public function countMounthlyPaymentsByFinancial($IDFinanceiro) {
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlTotalMounthlyPaymentsByFinancial($IDFinanceiro);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberOfMounthlyPaymentsReleased');
    }

    public function countBillPaymentByFinancial($IDFinanceiro) {
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlTotalPaymentTypeByFinancial($IDFinanceiro, 'BOLETO');
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberOfPayments');
    }

    public function countCreditCardPaymentByFinancial($IDFinanceiro) {
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlTotalPaymentTypeByFinancial($IDFinanceiro, 'CARTAO');
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberOfPayments');
    }

    public function countFrozenTimesByFinancial($IDFinanceiro) {
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlTotalSituationByFinancial($IDFinanceiro, 'CONGELADO');
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberOfSituation');
    }

    public function countPreFrozenTimesByFinancial($IDFinanceiro) {
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlTotalSituationByFinancial($IDFinanceiro, 'PRE CONGELADO');
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberOfSituation');
    }

    public function countDelayTimesByFinancial($IDFinanceiro) {
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlTotalSituationByFinancial($IDFinanceiro, 'ATRASO');
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberOfSituation');
    }

    public function reverseCustomer($IDCliente, $FoValor, $DtData) {
        $this->initConnection();
        $this->tMysqli->escape($IDCliente);
        $this->tMysqli->escape($FoValor);
        $this->tMysqli->escape($DtData);
        $StSql = TQuery::getSqlInsertReverse($IDCliente, $FoValor, $DtData);
        $this->tMysqli->exec($StSql);
        $BoRetorno = $this->tMysqli->lastQueryReturn();
        $this->closeConnection();
        return $BoRetorno;
    }

    public function findPendingPaymentsByFinancial($IDFinanceiro) {
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlFindPendingReleasesByFinancial($IDFinanceiro);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArReleases = array();
        while($ArReleaseData = mysqli_fetch_assoc($tQuery)) {
            $ArReleases[] = new Lancamento($ArReleaseData, $this->tMysqli);
        }
        $this->closeConnection();
        return $ArReleases;
    }

    public function releasePayment($ArPayments, $IDFinanceiro) {
        $Customer = $this->clienteService->findByFinancial($IDFinanceiro);
        $this->initTransaction();
        $this->tMysqli->escape($IDFinanceiro);
        $this->tMysqli->escape($ArPayments);
        foreach($ArPayments as $IDPayment => $ArPaymentData) {
            $StSql = TQuery::getSqlInsertPaymentConfirmation($IDPayment, $ArPaymentData);
            $this->tMysqli->exec($StSql);
        }
        if( $this->tMysqli->isCommitable() && $this->isCustomerWillBeAcquitted($Customer, count($ArPayments))) {
            $StSqlInsertHistorySituation = TQuery::getSqlInsertHistorySituation($Customer->getFinanceiro()->getIDFinanceiro(), 'LIBERADO');
            $this->tMysqli->exec($StSqlInsertHistorySituation);
            $StSqlUpdateStatusCustomer = TQuery::getSqlUpdateStatusCustomer($IDFinanceiro, 'NORMAL');
            $this->tMysqli->exec($StSqlUpdateStatusCustomer);
        }
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    private function isCustomerWillBeAcquitted($Customer, $ItNumberOfPayments) {
        return $Customer->countDelayedReleases() - $ItNumberOfPayments == 0;
    }

    public function findValueActiveCustomerInSystem($ItLimit) {
        $this->initConnection();
        $this->tMysqli->escape($ItLimit);
        $StSql = TQuery::getSqlGetValueActiveCustomerInSystem($ItLimit);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while( $ArCustomerData = mysqli_fetch_assoc($tQuery) ) {
             $Customer = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
             $Customer->setReport( array('Total' => MathUtils::money( $ArCustomerData['Total'] )) );
             $ArCustomers[] = $Customer;
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    public function update($ArDados) {
        $this->initConnection();
        $this->tMysqli->escape($ArDados);
        $StSql = TQuery::getSqlUpdateFinancialById($ArDados);
        $this->tMysqli->exec($StSql);
        $BoReturn = $this->tMysqli->lastQueryReturn();
        $this->closeConnection();
        return $BoReturn;
    }

}

?>
